from datetime import datetime

import pluggy
import pytest

# from py._xmlgen import html
# from _pytest import runner
from py.xml import html

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    # print('------------------------------------')

    # 获取钩子方法的调用结果
    out = yield
    print('用例执行结果', out)

    # 3. 从钩子方法的调用结果中获取测试报告
    report = out.get_result()
    print(report)  # 输出结果： <TestReport 'testSuite/pre_test/mallHome_test.py::test_SpuDetailState' when='setup' outcome='passed'>

    print('测试报告：%s' % report)
    print('步骤：%s' % report.when)
    print('nodeid：%s' % report.nodeid)
    global  description
    description = str(item.function.__doc__)


    print('description:%s' % description)
    print("测试函数名称：",item.name)
    global runResult
    runResult = report.outcome
    print(('运行结果: %s' % runResult))
def pytest_configure(config):  # 测试报告 Environment 部分配置测试地址 该函数只有在命令行运行才不会报错
    config._metadata['测试地址'] = '192.168.3.XXX'

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p("测试人: 超好播测试组")])

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('description'))
    cells.insert(2, html.th('Test'))  #增加一列，列名 Test，解决用例参数入参为中文时正常显示
    cells.insert(3, html.th('Time', class_='sortable time', col='time'))
    cells.pop(4) #将原有的test删除。
    cells.pop(5)

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    # cells.insert(1, html.td(description))
    cells.insert(1, html.td("订单支付流程"))
    # cells.insert(2, html.td(report.nodeid))             # report.nodeid 节点名称
    cells.insert(2, html.td(report.nodeid))
    cells.insert(3, html.td(datetime.utcnow(), class_='col-time'))
    cells.pop(4)
    cells.pop(5)


@pytest.fixture(scope="session",autouse=True)                     # scope='session'
def init_driver():

    yield

